import logging
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

from public.tools import basedir


class Base:
    def __init__(self,d):
        self.driver=d
        # 便于带出方法，生成后删除
        #self.driver=webdriver.Edge()

    def open_url(self,u):
        self.driver.get(u)

    # 普通定位
    def locate_element(self, loc):
        try:
            # 加*解除（）
            return self.driver.find_element(*loc)
        except Exception as e:
            print("未定位到元素变量值：", loc)
            logging.error(e)
            return None

    def loc_eles(self,loc):

        return self.driver.find_elements(*loc)


    def loc_wait_ele(self, loc):
        try:
            return WebDriverWait(self.driver, 10, 1).until(
                # 无需加*，传递的为元组
                EC.presence_of_element_located(loc)
            )
        except Exception as e:
            print("超时，未定位到元素变量值：", loc)
            logging.error(e)
            return None

    # 切iframe
    def s_t_iframe(self, loc):
        try:
            l = self.locate_element(loc)
            return self.driver.switch_to.frame(l)
        except Exception as e:
            print("未定位到iframe元素变量值：", loc)
            logging.error(e)
            return None

    # 切出iframe
    def s_b_iframe(self):
        return self.driver.switch_to.default_content()

    # 窗口跳转-N
    def c_w(self):
        return self.driver.switch_to.window(self.driver.window_handles[-1])

    # 窗口回
    def c_w_t(self):
        return self.driver.switch_to.window(self.driver.window_handles[0])

    # 下拉
    def scroll_d(self):
        return self.driver.execute_script('window.scrollBy(0,800);')

    # 悬停封装
    def hover_ele(self, el):
        ele = self.locate_element(el)
        return ActionChains(self.driver).move_to_element(ele).perform()

    #双击
    def double_click(self,loc):
        ele=self.loc_wait_ele(loc)
        ActionChains(self.driver).double_click(ele).perform()

    #右击
    def right_click(self,loc):
        ele=self.loc_wait_ele(loc)
        ActionChains(self.driver).context_click(ele).click()

    #键盘事件回车
    def enter(self,loc):
        ele=self.loc_wait_ele(loc)
        ele.send_keys(Keys.ENTER)

    #键盘事件-全选
    def select_all(self,loc):
        ele=self.loc_wait_ele(loc)
        ele.send_keys(Keys.CONTROL,'A')

    # 键盘事件-复制
    def copy(self, loc):
        ele = self.loc_wait_ele(loc)
        ele.send_keys(Keys.CONTROL, 'C')

    # 键盘事件-粘贴
    def pause(self, loc):
        ele = self.loc_wait_ele(loc)
        ele.send_keys(Keys.CONTROL, 'V')

    #灵活切窗口
    def switch_window(self,n):
        self.driver.switch_to.window(self.driver.window_handles[n])

    #弹窗处理
    def alert_ensure(self):
        self.driver.switch_to.alert.accept()

    def alert_dismiss(self):
        self.driver.switch_to.alert.dismiss()

    #下拉框选择
    def select_item(self,loc,index):
        ele=self.loc_wait_ele(loc)
        Select(ele).select_by_index(index=index)


    def capturepic(self,filename):
        t=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot(basedir+r"/data/"+t+filename)












